﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO = Microsoft.MSE.Catalog.Contracts;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace Microsoft.MSE.Catalog.Module.Data
{
    public class Actor
    {      
        public Actor()
        { }
            public static Guid CreateActor(DTO.Actor actor, SqlTransaction txn, SqlConnection conn)
                {
                    Guid ID;
                    bool autoclose = false;
                    const string sprocname = "[ServiceEngine].[CreateActor]";

                    try
                    {
                        if (conn == null) conn = CatalogSvcHelper.GetConnection();
                        if (conn.State == ConnectionState.Closed)
                        {
                            autoclose = true;
                            conn.Open();
                        }

                        using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                        {
                            SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                            SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                            SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);
                            
                            cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Direction = ParameterDirection.Output;
                        NameParam.Value = actor.Name;
                        
                        if (actor.Description != null)
                        {
                            DescriptionParam.Value = actor.Description;
                        }
                        else
                        {
                            DescriptionParam.Value = DBNull.Value;
                        }
                           
                        cmd.Parameters.Add(IDParam);
                        cmd.Parameters.Add(NameParam);                                          
                        cmd.Parameters.Add(DescriptionParam);

                            cmd.ExecuteNonQuery();
                            ID = (Guid)IDParam.Value;
                        }

                    }
                    finally
                    {
                        if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                    }

                    return ID;
                }

            public static DTO.Actor UpdateActor(DTO.Actor actor, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                const string sprocname = "[ServiceEngine].[UpdateActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                        SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);                    
                        SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);                   

                        cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Value = actor.ID;                    
                        NameParam.Value = actor.Name;                    
                        
                        if (actor.Description != null)
                        {
                            DescriptionParam.Value = actor.Description;
                        }
                        else
                        {
                            DescriptionParam.Value = DBNull.Value;
                        }             

                        cmd.Parameters.Add(IDParam);                   
                        cmd.Parameters.Add(NameParam);                    
                        cmd.Parameters.Add(DescriptionParam);                  

                        cmd.ExecuteNonQuery();
                    }
                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

                return actor;
            }

            public static void DeleteActor(Guid ID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                const string sprocname = "[ServiceEngine].[DeleteActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Value = ID;
                        cmd.Parameters.Add(IDParam);

                        cmd.ExecuteNonQuery();
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

            }

            public static DTO.Actor GetActor(Guid ID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                DTO.Actor process = new DTO.Actor();
                const string sprocname = "[ServiceEngine].[GetActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Value = ID;
                        cmd.Parameters.Add(IDParam);
                    
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                reader.Read();
                                process.ID = reader.GetGuid(0);
                                process.Name = reader.GetString(1);                            
                                                          
                                if (reader.IsDBNull(2))
                                {
                                    process.Description = null;
                                }
                                else
                                {
                                    process.Description = reader.GetString(2);
                                }                                
                                
                            }
                        }
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }


                return process;

            }

            public static DTO.ActorList ListActors(SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                DTO.ActorList list = new DTO.ActorList();
                const string sprocname = "[ServiceEngine].[ListActors]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                list.Actors = new List<DTO.ActorListElement>();
                                while (reader.Read())
                                {
                                    DTO.ActorListElement element = new DTO.ActorListElement();
                                    element.ID = reader.GetGuid(0);
                                    element.Name = reader.GetString(1);
                                    if (reader.IsDBNull(2))
                                    {
                                        element.Description = null;
                                    }
                                    else
                                    {
                                        element.Description = reader.GetString(2);
                                    }
                                    list.Actors.Add(element);
                                }
                            }
                        }
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

                return list;

            }

            public static DTO.CapabilityList ListCapabilitiesForActor(Guid actorID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                DTO.CapabilityList list = new DTO.CapabilityList();
                const string sprocname = "[ServiceEngine].[ListCapabilitiesForActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter IDParam = new SqlParameter("@ActorID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Value = actorID;
                        cmd.Parameters.Add(IDParam);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                list.Capabilities = new List<DTO.CapabilityListElement>();
                                while (reader.Read())
                                {
                                    DTO.CapabilityListElement element = new DTO.CapabilityListElement();
                                    element.ID = reader.GetGuid(0);
                                    element.Name = reader.GetString(1);
                                    if (reader.IsDBNull(2))
                                    {
                                        element.Description = null;
                                    }
                                    else
                                    {
                                        element.Description = reader.GetString(2);
                                    }
                                    list.Capabilities.Add(element);
                                }
                            }
                        }
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

                return list;

            }

            public static DTO.SLEList ListSLEsForActor(Guid actorID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                DTO.SLEList list = new DTO.SLEList();
                const string sprocname = "[ServiceEngine].[ListSLEsForActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter IDParam = new SqlParameter("@ActorID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Value = actorID;
                        cmd.Parameters.Add(IDParam);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                list.SLEs = new List<DTO.SLEListElement>();
                                while (reader.Read())
                                {
                                    DTO.SLEListElement element = new DTO.SLEListElement();
                                    element.ID = reader.GetGuid(0);
                                    element.Name = reader.GetString(1);
                                    if (reader.IsDBNull(2))
                                    {
                                        element.Description = null;
                                    }
                                    else
                                    {
                                        element.Description = reader.GetString(2);
                                    }
                                    list.SLEs.Add(element);
                                }
                            }
                        }
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

                return list;

            }
            
            public static List<DTO.ActorListElement> ListActorsByGroup(Guid entityGroupID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                List<DTO.ActorListElement> list = new List<DTO.ActorListElement>();
                const string sprocname = "[ServiceEngine].[ListEntitiesByGroup]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter IDParam = new SqlParameter("@entityGroupID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        IDParam.Value = entityGroupID;
                        cmd.Parameters.Add(IDParam);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                list = new List<DTO.ActorListElement>();
                                while (reader.Read())
                                {
                                    DTO.ActorListElement element = new DTO.ActorListElement();
                                    element.ID = reader.GetGuid(0);
                                    element.Name = reader.GetString(1);
                                    if (reader.IsDBNull(2))
                                    {
                                        element.Description = null;
                                    }
                                    else
                                    {
                                        element.Description = reader.GetString(2);
                                    }

                                    list.Add(element);
                                }
                            }
                        }
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

                return list;

            }

            public static void AssignCapabilityToActor(Guid actorID, Guid capabilityID, Guid sleID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                const string sprocname = "[ServiceEngine].[AssignCapabilityToActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter CapabilityIDParam = new SqlParameter("@CapabilityID", SqlDbType.UniqueIdentifier);
                        SqlParameter ActorIDParam = new SqlParameter("@ActorID", SqlDbType.UniqueIdentifier);
                        SqlParameter SLEIDParam = new SqlParameter("@SLEID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        CapabilityIDParam.Value = capabilityID;
                        ActorIDParam.Value = actorID;

                        if (sleID != Guid.Empty)
                            SLEIDParam.Value = sleID;
                        else
                            SLEIDParam.Value = null;

                        cmd.Parameters.Add(CapabilityIDParam);
                        cmd.Parameters.Add(ActorIDParam);
                        cmd.Parameters.Add(SLEIDParam);

                        cmd.ExecuteNonQuery();
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }
            }

            public static void RemoveCapabilityFromActor(Guid actorID, Guid capabilityID, SqlTransaction txn, SqlConnection conn)
            {
                bool autoclose = false;
                const string sprocname = "[ServiceEngine].[RemoveCapabilityFromActor]";

                try
                {
                    if (conn == null) conn = CatalogSvcHelper.GetConnection();
                    if (conn.State == ConnectionState.Closed)
                    {
                        autoclose = true;
                        conn.Open();
                    }

                    using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                    {
                        SqlParameter CapabilityIDParam = new SqlParameter("@CapabilityID", SqlDbType.UniqueIdentifier);
                        SqlParameter ActorIDParam = new SqlParameter("@ActorID", SqlDbType.UniqueIdentifier);

                        cmd.CommandType = CommandType.StoredProcedure;
                        CapabilityIDParam.Value = capabilityID;
                        ActorIDParam.Value = actorID;

                        cmd.Parameters.Add(CapabilityIDParam);
                        cmd.Parameters.Add(ActorIDParam);

                        cmd.ExecuteNonQuery();
                    }

                }
                finally
                {
                    if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
                }

            }

        }
    }
